/**
 * Created by jianglinjie on Fri Jun 22 2018
 */
'use strict';
const fs = require('fs');
const path = require('path');
const basename  = path.basename(__filename);
const Sequelize = require('sequelize');
const { seq: config } = require('../config');

console.log('Sequelize config:', config);

const sequelize = new Sequelize(config.db, config.user, config.pass, {
  dialect: 'mysql',
  host: config.host,
  port: config.port,
});

let models = {};

fs
  .readdirSync(__dirname)
  .filter(file => {
    console.log('slice test:', file.slice(-3));
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    let model = sequelize.import(path.join(__dirname, file));
    models[model.name] = model;
  });

Object.keys(models).forEach(modelName => {
  if (models[modelName].associate) {
    models[modelName].associate(models);
  }
});

models.sequelize = sequelize;
models.Sequelize = Sequelize;

module.exports = models;